.EXPORT_ALL_VARIABLES:

.PHONY: clean all

ROOTCFLAGS   := $(shell root-config --cflags)
ROOTLIBS     := $(shell root-config --libs)
ROOTGLIBS    := $(shell root-config --glibs)
ROOTINC      := -I$(shell root-config --incdir)

COMMON_DIR = ../../Common
MBSIO_DIR = ../Unpacker/UnpackerMesy
MACRO_DIR = ../Macros
bin_DIR = ../../bin
lib_DIR = ../../lib

ALLIBS  = -lm $(ROOTLIBS) $(ROOTGLIBS) -L$(COMMON_DIR) -lCommandLineInterface -lTransferEventBuilder -lSpectrum

CC		= gcc
CPP             = g++
CFLAGS		= -g -O2 -pipe -Wall -Wextra -W -Woverloaded-virtual $(ROOTCFLAGS)

INCLUDES        = -I./ -I$(MBSIO_DIR) -I$(COMMON_DIR) -I$(MACRO_DIR)
LFLAGS		= -O2 -g 
LIBS 		= $(ALLIBS)
SOFLAGS 	= -dynamiclib -single_module -undefined dynamic_lookup

O_FILES = Barrel.o \
	Annular.o \
	Germanium.o \
	CalEvent.o \
	CalibrateDictionary.o \
	Calibration.o \
	Mux.o \
	Settings.o \
	SRIMloader.o \
	MBangle.o

LIBRARIES = $(COMMON_DIR)/libCommandLineInterface.a \
#	$(LIB_DIR)/libMacro.so \
#	$(ROOTDIR)/libSpectrum.so

all: Calibrate Template Cal_histos MyCal_histos PeakFind CD_histos CDPeaks For_histos Coin_histos Ne_histos
	@echo Done

#Arborist: Arborist.cc $(O_FILES) $(LIBRARIES)
#	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@

Calibrate: Calibrate.cc $(O_FILES) $(LIBRARIES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
#	$(CPP) $(LFLAGS) $(SOFLAGS) -install_name libCalibrate.so -o $(LIB_DIR)/libCalibrate.so $(O_FILES) -lc
	cp Calibrate $(bin_DIR)	

Template: Template.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp Template $(bin_DIR)	

Cal_histos: Cal_histos.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp Cal_histos $(bin_DIR)	

MyCal_histos: MyCal_histos.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp MyCal_histos $(bin_DIR)	

PeakFind: PeakFind.cc $(COMMON_DIR)/libMacro.a $(COMMON_DIR)/libCommandLineInterface.a
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp PeakFind $(bin_DIR)	

CD_histos: CD_histos.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp CD_histos $(bin_DIR)	

CDPeaks: CDPeaks.cc $(COMMON_DIR)/libMacro.a $(COMMON_DIR)/libCommandLineInterface.a
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp CDPeaks $(bin_DIR)	

For_histos: For_histos.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp For_histos $(bin_DIR)	

Coin_histos: Coin_histos.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp Coin_histos $(bin_DIR)	

Ne_histos: Ne_histos.cc $(O_FILES)
	$(CPP) $(CFLAGS) $(INCLUDES) $^ $(LIBS) -o $@
	cp Ne_histos $(bin_DIR)	

%.o: %.cc %.hh
	@echo Default .o rule
	$(CPP) $(CFLAGS) $(INCLUDES) -c $< -o $@

#---- Root stuff

DEPENDENCIES = Barrel.hh \
	Annular.hh \
	Germanium.hh \
	CalEvent.hh \
	Calibration.hh \
	Mux.hh \
	Settings.hh \
	RootLinkDef.h

CalibrateDictionary.o: CalibrateDictionary.cc CalibrateDictionary.h
	 $(CPP) -fPIC $(CFLAGS) $(INCLUDES) -c $<

CalibrateDictionary.cc: $(DEPENDENCIES)
	 rm -f CalibrateDictionary.cc CalibrateDictionary.h; rootcint -f $@ -c $(INCLUDES) $(DEPENDENCIES)


#--------------------------------------------------
clean:
	rm -f Calibrate Template Cal_histos *.o CDPeaks CD_histos Coin_histos For_histos MyCal_histos MyCal_histos_Zn Ne_histos PeakFind SimCal_histos CalibrateDictionary.*

tar:
	tar -chvzf Calibrate.tar.gz Annular.cc Annular.hh Barrel.cc Barrel.hh CalEvent.cc CalEvent.hh Cal_histos.cc Calibrate.cc Calibration.cc Calibration.hh Germanium.cc Germanium.hh Makefile Mux.cc Mux.hh Time.hh RootLinkDef.h Settings.cc Settings.hh settings.dat cal2009.dat
